//
//  DENSearchViewController.m
//  Icar
//
//  Created by apple on 15/10/26.
//  Copyright © 2015年 小龙侠. All rights reserved.
//

#import "DENSearchViewController.h"
#import "BaseHeader.h"
@interface DENSearchViewController ()<UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating, UISearchBarDelegate>
@property (strong, nonatomic)UITableView *tableView;//显示内容
@property (strong, nonatomic)NSMutableArray *searchArr;//搜索结果的数组
@property (strong, nonatomic)UISearchController *searchController;//搜索框
@property (strong, nonatomic)NSMutableArray *allDataArr;
@end

@implementation DENSearchViewController

- (void)viewWillAppear:(BOOL)animated
{
    [self.searchController.searchBar setHidden:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self setAutomaticallyAdjustsScrollViewInsets:YES];
//    self.navigationController.navigationBar.hidden = YES;
    self.searchArr = [NSMutableArray array];
    self.allDataArr = [NSMutableArray array];
    [self creatTableVeiw];
    [self creatSearchViewController];
}
#pragma mark- 创建search
- (void)creatSearchViewController
{
    self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
//   签订 UISearchResultsUpdating 代理
    self.searchController.searchResultsUpdater = self;
//    设置开始搜索时背景是否显示 页面阴影效果 一般设置为no;
    self.searchController.dimsBackgroundDuringPresentation = NO;
//    点击时隐藏搜索栏
    self.searchController.hidesNavigationBarDuringPresentation = YES;
//    设置searchbar的位置大小
    self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 40.);
    self.searchController.searchBar.placeholder = @"请输入要搜索的内容";
    [self.searchController.searchBar setShowsCancelButton:YES animated:NO];
//    自适应位子
    [self.searchController.searchBar sizeToFit];
    [self.view addSubview:self.searchController.searchBar];
    self.searchController.searchBar.delegate = self;
    self.tableView.tableHeaderView = self.searchController.searchBar;
}
#pragma mark- 请求数据
- (void)requestData
{
    [self.allDataArr removeAllObjects];
    NSString *text = [self.searchController.searchBar text];
    NSLog(@"text sfsdfsdfds %@", text);
    text = [text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSString *str = [NSString stringWithFormat: @"http://mrobot.pcauto.com.cn/v3/search/tips?keyword=%@", text];
    NSLog(@"ikhkhhl %@", str);
    [AFNBlock getDataWithString:str block:^(id back) {
        if (back == nil) {
            [self hintView];
        }else
        {
            for (NSDictionary *dic in back) {
                DENFindModel *findModel = [[DENFindModel alloc]init];
                [findModel setValuesForKeysWithDictionary:dic];
                if (findModel.price != nil) {
                    
                    [self.allDataArr addObject:findModel];
                }
            }
        }
        [self.tableView reloadData];
        NSLog(@"%ld", self.allDataArr.count);
    }];
}
#pragma mark- 提示view
- (void)hintView
{
    UIView *hintView = [[UIView alloc]initWithFrame:CGRectMake(WIDTH(80), HEIGHT(300), WIDTH(200), HEIGHT(80))];
    UILabel *hintLabel = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH(5), HEIGHT(5), WIDTH(180), HEIGHT(70))];
    hintLabel.text = @"输入的内容格式不对, 或者搜索的内容不存在";
    [hintView addSubview:hintLabel];
    [self.view addSubview:hintView];
}
#pragma mark- 创建tableVeiw
- (void)creatTableVeiw
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = NO;
    [self.view addSubview:self.tableView];
}
#pragma mark- cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    if (self.searchController.active == YES) {
        DENFindModel *model = [[DENFindModel alloc]init];
       model = [self.allDataArr objectAtIndex:indexPath.row];
        if (model.price != nil) {
            cell.textLabel.text = model.keyword;
            cell.detailTextLabel.text = model.price;
        }
        
    }
    return cell;
}
#pragma mark- 每个section中的cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"22  %ld", self.searchArr.count);
    return self.allDataArr.count;
}
#pragma mark- cell点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DENFindDetailViewController *detailVC = [[DENFindDetailViewController alloc]init];
    [self.navigationController pushViewController:detailVC animated:YES];
    DENFindModel *model = [[DENFindModel alloc]init];
    model = [self.allDataArr objectAtIndex:indexPath.row];
    detailVC.did = model.sid;
    ;
    if (self.searchController.active) {
        self.searchController.active = NO;
        [self.searchController.searchBar setHidden:YES];
    }
    
}
#pragma mark- search代理方法
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
//    NSString *string = [self.searchController.searchBar text];
//    NSPredicate *predica = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", string];
//    self.searchArr = [NSMutableArray arrayWithArray:[self.allDataArr filteredArrayUsingPredicate:predica]];
    
//    [self.tableView reloadData];
   // NSLog(@"sdfs %ld", self.searchArr.count);
    self.tableView.separatorStyle = YES;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self requestData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
